package utils;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 08 - 01
 * Time:17:41
 */

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/***
 * 统一的JSON处理类
 */
public class ResultJSONUtils {
    /***
     * 给前端输出JSON数据
     * @param response
     * @param data
     */
    public static void write(HttpServletResponse response, HashMap<String,Object> data) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");

        PrintWriter writer = response.getWriter();
        //将data对象转化为JSON
        ObjectMapper mapper = new ObjectMapper();
        writer.println(mapper.writeValueAsString(data));
    }

}
